package com.mzw.huaxiarealestatebackend.screen;

import com.mzw.huaxiarealestatebackend.common.utils.DateFormat;
import com.mzw.huaxiarealestatebackend.entity.Screenshot;
import com.mzw.huaxiarealestatebackend.entity.Staff;
import com.mzw.huaxiarealestatebackend.service.ScreenshotService;
import com.mzw.huaxiarealestatebackend.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

@Component
public class ScheduledTask {

    //@Value("${nircmd.path}")
    private String nircmdPath = "D:\\\\nircmd\\\\NirSoft\\\\nircmd.exe";

    private String userId;

    @Autowired
    private ScreenshotService screenshotService;

    @Autowired
    private StaffService staffService;

    // 定时截图任务，每15分钟执行一次
    @Scheduled(fixedDelay = 15 * 60 * 1000)
    public void captureScreenshotTask() {
        if (userId != null) {
            try {

                Staff staff = this.staffService.queryById(userId);
                // 生成随机文件名
                //String randomFilename = UUID.randomUUID().toString() + ".png";
                String randomFilename;
                // 生成当前时间和用户ID的组合作为文件名
                LocalDateTime currentTime = LocalDateTime.now();
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
                randomFilename = currentTime.format(formatter) + "_" + staff.getStfName() + ".png";

                // 构建保存截图的文件路径，可以使用时间戳或其他方式生成唯一的文件名
                //String savePath = "E:\\demos\\realty-admin\\huaxia-real-estate-backend\\pictures\\" + randomFilename;

                String savePath = "./pictures\\" + randomFilename;

                // 构建 nircmd 命令
                String nircmdCommand = nircmdPath + " savescreenshot " + savePath;

                // 执行命令
                Process process = Runtime.getRuntime().exec(nircmdCommand);

                // 你可能需要等待命令执行完成
                process.waitFor();

                System.out.println("Screenshot captured successfully. Filename: " + randomFilename);

                // 模拟获取截图信息
                Screenshot screenshotEntity = new Screenshot();

                screenshotEntity.setUserId(userId);
                screenshotEntity.setFilename(randomFilename);
                screenshotEntity.setFilepath(savePath);
                screenshotEntity.setTimestamp(DateFormat.getNowDateFormat());

                // 保存截图信息到数据库
                screenshotService.save(screenshotEntity);

                // 重置用户ID，避免重复截图
                //userId = null;
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public void setUserId(String newUserId) {
        userId = newUserId;
    }

    public void triggerScreenshotTask() {
        captureScreenshotTask();  // 手动触发
    }
}